
import java.awt.*;
import java.applet.Applet;

public class UserInterface extends Applet implements Runnable {

/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
Thread runner;
Image bufferImg;
Graphics bufferG;
int color;
int xcoord;
int ycoord;
int speed;
String code;

private World antWorld = new World(Color.white);


public void init() {

   bufferImg = createImage(this.getSize().width, this.getSize().height);
   bufferG = bufferImg.getGraphics();
   
   String sx = getParameter("xStart");
   if (sx == null) xcoord = 100;
   else xcoord = Integer.parseInt(sx);
   
   String sy = getParameter("yStart");
   if (sy == null) ycoord = 100;
   else ycoord = Integer.parseInt(sy);
   
   String spd = getParameter("Speed");
   if (spd == null) speed = 40;
   else speed = Integer.parseInt(spd);
}

public void start() { 
   if (runner == null); { 
     runner = new Thread(this); 
     runner.start(); 
   } 
} 
 
public void stop() { 
   if (runner != null) { 
      runner.interrupt();
      runner = null; 
   } 
} 
 
public void run() { 
   while (true) {
      astep();
      try { Thread.sleep(1000/speed); }
      catch (InterruptedException e) { }
   }
}

public void astep() {
	antWorld.step();
    repaint();
}

public void update(Graphics g) {
   paint(g);
}

public void paint(Graphics g) {
	
	
	if(antWorld.getField(antWorld.getAnts().get(0).getPosX(), antWorld.getAnts().get(0).getPosY()).getIsActive())
	{
		   bufferG.setColor(new Color(0,0,0));
		   bufferG.fillRect(antWorld.getField(antWorld.getAnts().get(0).getPosX(),antWorld.getAnts().get(0).getPosY()).getPosX()*3,antWorld.getField(antWorld.getAnts().get(0).getPosX(),antWorld.getAnts().get(0).getPosY()).getPosY() * 3,3,3);
		   g.drawImage(bufferImg,0,0,this);
		   
	}else{
		   bufferG.setColor(new Color(250,250,250));
		   bufferG.fillRect(antWorld.getField(antWorld.getAnts().get(0).getPosX(),antWorld.getAnts().get(0).getPosY()).getPosX()*3,antWorld.getField(antWorld.getAnts().get(0).getPosX(),antWorld.getAnts().get(0).getPosY()).getPosY() * 3,3,3);
		   g.drawImage(bufferImg,0,0,this);
	}

}

}
